layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    formSelects: 'formSelects-v4',
    xmSelect: "xm-select"
}).use(['jquery', 'formSelects', 'form','layer','xmSelect'], function () {
    const form = layui.form
        , layer = layui.layer
        , xmSelect = layui.xmSelect
        , $ = layui.jquery;
    $("#close").on('click', function () {
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    form.on('submit(save)', function (data) {
        const params = data.field;
        let selectUsersData = selectUsers.getValue()
        let user = ''
        for (let i = 0; i < selectUsersData.length; i++) {
            let value = selectUsersData[i].value;
            user+=value+","
        }
        user = user.substring(0,user.length - 1)
        params.teamMembersMail = user
        submit($, params);
        return false;
    });
    $(function (){
        getAllUsers()
    })
    let selectUsers
    function getAllUsers() {
        $.ajax({
            url: '/admUser/getAll',
            dataType: "json",
            type: "GET",
            success: function (result) {
                const allUsers =[]
                const data = result.data
                for (let i = 0; i < data.length; i++) {
                    const user = {
                        name :data[i].realName,
                        value : data[i].mail
                    };
                    allUsers.push(user);
                }
                //渲染数据
                selectUsers = xmSelect.render({
                    el: '#teamMembersMail',
                    data: allUsers,
                })
            }
        })
    }

    function submit($, params) {
        $.ajax({
            url: '/admProject',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "POST",
            success: function (result) {
                layer.msg(result.data, {icon: 1, time: 1000}, function () {
                    CloseWin();
                })
            }
        })
    }
    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

});

